<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 字典表操作ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */
quanli_require('module/base/system/itemHandler.class.php');

class ajax_basicSetting {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1: //查询基础设置分类列表
                    $this->searchItemCategory();
                    break;
                case 2:  //查询基础设置分类详情（单个）
                    $this->searchItemCategoryDetail();
                    break;
                case 3: //查询分类下的选项信息
                    $this->searchItem();
                    break;
                case 4:// 删除选项分类
                    $this->delItem();
                    break;
                case 5://启用或禁用选项分类
                    $this->changeItemStatus();
                    break;
                case 6://查询基础设置分类选项详情
                    $this->searchItemDetail();
                    break;
                case 100://编辑和添加
                    $this->itemCategoryEdit();
                    break;
                case 101://添加或编辑选项分类
                    $this->itemEdit();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    /**
    * 查询字典信息
    */
    private function searchItemCategory() {

        global $G_PGLOB;

        //实例化字典表操作类
        $itemHandler = new itemHandler( $G_PGLOB->DBCA );

        //调用查询字典信息方法
        $itemList = $itemHandler->searchCategory( $_REQUEST );

        //组织返回的数据
        $result_array = array(
        'page' => $_REQUEST['page'],
        'total' => $_REQUEST['count'],
        'rows' => array()
        );

        if ( $itemList ) {
            foreach ( $itemList as $item ) {
                //编辑字符串
                $editString = '';
                $editString .='<a onclick="basicSetting.edit( ' . $item['itemCategoryID'] . ' )" href="javascript: void(0);">修改</a>&nbsp;&nbsp;&nbsp;';
                $editString .='<a onclick="basicSetting.option.search( ' . $item['itemCategoryID'] . ' )" href="javascript: void(0);">选项</a>&nbsp;&nbsp;&nbsp;';
                $result_array['rows'][] = array(
                    'id' => $item['itemCategoryID'],
                    'cell' => array(
                    $editString,
                    $item['itemCategoryID'],
                    $item['itemCategoryName'],
                    )
                );
            }
        }
    echo json_encode( $result_array );
    }

    /**
    * 查询基础设置分类详情（单个）
    * @global type $G_PGLOB
    * @return type
    */
    private function searchItemCategoryDetail() {

        global $G_PGLOB;

        if (!isset($_REQUEST['itemCategoryID']) && quanli_is_int($_REQUEST['itemCategoryID'])) {
            returnJsonData('参数错误');
        }

        //实例基础设置类
        $itemHandler = new itemHandler();

        $args_array = array(
            'itemCategoryID' => $_REQUEST['itemCategoryID']
        );
        $item_category_array = $itemHandler->searchCategory($args_array);
        if ($item_category_array) {
            returnJsonData('ok', 200, $item_category_array[0]);
        } else {
            returnJsonData('分类信息没有找到', 404);
        }
    }

    /**
    *  查询分类下的选项信息
    */
    private function searchItem() {

        global $G_PGLOB;

        if (!isset($_REQUEST['itemCategoryID']) && quanli_is_int($_REQUEST['itemCategoryID'])) {
            returnJsonData('参数错误');
        }

        //实例基础设置类
        $itemHandler = new itemHandler();

        //查询选项
        $args_array = array(
            'itemCategoryID' => $_REQUEST['itemCategoryID']
        );

        //判断是否为切分（编号为8）从小到大排序
        if( $_REQUEST['itemCategoryID'] ==8 ){
            $args_array = array(
                'itemCategoryID' => $_REQUEST['itemCategoryID'],
                'extend'=>' ORDER BY CAST(itemName AS SIGNED) ASC '
            );
        }
        $item_array = $itemHandler->searchItem($args_array);

        returnJsonData('ok', 200, $item_array);
    }

    /**
    * 删除选项分类信息
    */
    private function delItem() {
        global $G_PGLOB;
        //接收参数
        $itemID = $_REQUEST['itemID'];

        //统计不存在的选项分类数目
        $errorNum = 0;
        //检查要删除的选项分类
        $sys_item = new entity('sys_item');
        $sys_item->itemID = $itemID;

        if (!$G_PGLOB->DBCA->fetch($sys_item)) {
            $errorNum++;  //记录不存在的选项分类数
        }
        if ($errorNum > 0) {
            returnJsonData('所选部分信息已不存在，请刷新页面', 400);
        }
        $G_PGLOB->DBCA->delete($sys_item);
        returnJsonData('删除成功', 200);
    }

    /**
    * 编辑分类名称
    */
    private function itemCategoryEdit() {

        global $G_PGLOB;

        //接收参数
        $itemCategoryID = $_REQUEST['itemCategoryID'];
        $itemCategoryName = $_REQUEST['itemCategoryName'];
        //检验参数格式
        if (empty($itemCategoryName)) {

            returnJsonData('分类名称不能为空', 400);
        }

        $sys_itemcategory = new entity('sys_itemcategory');

        if (!empty($itemCategoryID)) {

            $sys_itemcategory->itemCategoryID = $itemCategoryID;

            if (!$G_PGLOB->DBCA->fetch($sys_itemcategory)) {

                returnJsonData('没有找到当前信息', 404);
            }
            //修改时检查是否重复
            $sql = "SELECT itemCategoryID FROM sys_itemcategory WHERE itemCategoryID != ? AND itemCategoryName = ?";
            $item_temp_array = $G_PGLOB->DBCA->stmt_query( $sql, 'is', array($itemCategoryID, $itemCategoryName) );

            if ( $item_temp_array ) {
                returnJsonData('分类名称已存在', 400);
            }
        } else {
            $sys_itemcategory->itemCategoryName = $itemCategoryName;

            //添加时是否重复
            if( $G_PGLOB->DBCA->fetch($sys_itemcategory) ){

                returnJsonData('分类名称已存在', 400);

            }
            $sys_itemcategory->creator = $G_PGLOB->currentUserID;
        }
        $sys_itemcategory->editor = $G_PGLOB->currentUserID;
        $sys_itemcategory->itemCategoryName = $itemCategoryName;
        $G_PGLOB->DBCA->save($sys_itemcategory);
        if ($itemCategoryID) {

            returnJsonData('更新成功', 200);

        } else {
            returnJsonData('保存成功', 200);
        }
    }

    /**
    * 启用或禁用选项分类
    */
    private function changeItemStatus() {

        global $G_PGLOB;

        $useFlg = $_REQUEST['useFlg'];
        if (isset($_REQUEST['itemID']) && quanli_is_int($_REQUEST['itemID'])) {

            $itemID = $_REQUEST['itemID'];
            $sys_item = new entity('sys_item');
            $sys_item->itemID = $itemID;
            $G_PGLOB->DBCA->fetch($sys_item);

        if ($useFlg == 1 && $sys_item->useFlg == 1) {//状态为1 启用 状态为0 禁用
            $sys_item->useFlg = 0;
            //add by allen.qiang 20141219 保存修改人信息 start
            $sys_item->editor = $G_PGLOB->currentUserName;
            //add by allen.qiang 20141219 保存修改人信息 end
            $G_PGLOB->DBCA->save($sys_item);
            returnJsonData('禁用成功', 200);
        } else if (($useFlg == 1 && $sys_item->useFlg == 0) || ($useFlg == 0 && $sys_item->useFlg == 1)) {

            returnJsonData('当前数据信息已改变，请刷新页面', 400);

        } else {
             $sys_item->useFlg = 1;
             //add by allen.qiang 20141219 保存修改人信息 start
             $sys_item->editor = $G_PGLOB->currentUserName;
             //add by allen.qiang 20141219 保存修改人信息 end
             $G_PGLOB->DBCA->save($sys_item);

             returnJsonData('启用成功', 200);
        }
     } else {
            returnJsonData('您输入的信息有误', 400);
    }
   }

    /**
    * 查询基础设置分类选项详情
    * @global type $G_PGLOB
    */
    private function searchItemDetail(){
        global $G_PGLOB;

        if (!isset($_REQUEST['itemID']) && quanli_is_int($_REQUEST['itemID'])) {
            returnJsonData('参数错误');
        }
        //实例基础设置类
        $itemHandler = new itemHandler();

        //查询选项
        $args_array = array(
            'itemID' => $_REQUEST['itemID']
        );
        $item_array = $itemHandler->searchItem($args_array);
        if ($item_array) {
             returnJsonData('ok', 200, $item_array[0]);
        } else {
             returnJsonData('选项信息没有找到',  404);
        }
    }

    /**
    * 添加或编辑选项分类
    */
    private function itemEdit() {

        global $G_PGLOB;

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        //根据itemID 判断 存在itemID更新 不存在添加
        if (!isset($_REQUEST['itemID']) && quanli_is_int($_REQUEST['itemID'])) {
            returnJsonData('参数错误');
        }
        //分类号
        if ( !isset( $_REQUEST['itemCategoryID'] ) && quanli_is_int($_REQUEST['itemCategoryID'] ) ) {
            returnJsonData('参数错误');
        }

        $itemName = trim( isset($_REQUEST['itemName'] ) ? $_REQUEST['itemName'] : "" );                //名称

        if ( $itemName === '' ) {
            returnJsonData('选项分类名称不能为空', 400);
        }
        $sys_item = new entity( 'sys_item' );

        if ( !empty( $_REQUEST['itemID'] ) ) {

            $sys_item->itemID = $_REQUEST['itemID'];

            if ( !$G_PGLOB->DBCA->fetch( $sys_item ) ) {
                returnJsonData('没有找到当前信息', 400);
            }
            if( $_REQUEST['itemCategoryID'] == 1){

                if( $sys_item->extend_1 ){
                    $extend_1_db = $sys_item->extend_1;
                }else{
                    $extend_1_db = '';
                }
            }

        } else {

            $sys_item->creator = $G_PGLOB->currentUserID;
            $sys_item->useFlg = 1;
        }

        //add by 2014-12-03 促销渠道加部门信息 xuhongshun  start

        if( $_REQUEST['itemCategoryID'] == 1){
            $sys_item->extend_1 = $_REQUEST['search_departmentID'];
            $sys_item->extend_2 = $_REQUEST['departmentName'];
        }
        //add by 2014-12-03 促销渠道加部门信息 xuhongshun  end

        $sys_item->itemName = $itemName;                                               //选项分类名称
        $sys_item->itemCategoryID = $_REQUEST['itemCategoryID'];                              //分类号
        //add by allen.qiang 20141219 保存修改人信息 start
        $sys_item->editor = $G_PGLOB->currentUserName;
        //add by allen.qiang 20141219 保存修改人信息 end
        $G_PGLOB->DBCA->save( $sys_item );

        //mod by 2014-12-04 促销渠道加部门信息 xuhongshun  start
        if( $_REQUEST['itemCategoryID'] == 1){
            $returnArr = array(
                'itemID'=>$G_PGLOB->DBCA->insert_id,
                'itemName'=>$itemName,
                'extend_1'=>$_REQUEST['search_departmentID'],
                'extend_1_db'=>$extend_1_db,
                'extend_2'=>$_REQUEST['departmentName'],
                'itemCategoryID'=>$_REQUEST['itemCategoryID']
            );
        }else{
            $returnArr = array(
                'itemID'=>$G_PGLOB->DBCA->insert_id,
                'itemName'=>$itemName,
                'itemCategoryID'=>$_REQUEST['itemCategoryID']
            );
        }
        //mod by 2014-12-04 促销渠道加部门信息 xuhongshun  end

        if ($_REQUEST['itemID']) {
            returnJsonData('更新成功', 200,$returnArr);
        } else {
            returnJsonData('保存成功', 200,$returnArr);
        }
    }

}
